<?php

class SiteController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		$criteria = new CDbCriteria();
      $criteria->addCondition('active = 1');
      $criteria->order = 'hot DESC,modify_date DESC,create_date DESC';
      $criteria->offset = 0;
      $criteria->limit = 23;
      $top_news = News::model()->findAll($criteria);
      //CVarDumper::dump($top_news,10,true);die();
      
      //Lấy tất cả các danh mục cha
      $criteria = new CDbCriteria();
      $criteria->addCondition('parent_id = 0');
      $criteria->order = 'position ASC';
      $list_cat_parent = Category::model()->findAll($criteria);
      $block_news = array();
      
      
      if(count($list_cat_parent) > 0){
         foreach($list_cat_parent as $item){ // Mỗi danh mục cha lấy ra 16 hoặc 8 tin bao gồm tin của cả các danh mục con
            $tmp['cat_name'] = $item->name;
            $childs = array($item->id);
            
            //Lấy id của các danh mục con dựa vào danh mục cha
            $list_child = Category::model()->findAllByAttributes(array(
               'parent_id' => $item->id,
            ));
            if(count($list_child) > 0){
               foreach($list_child as $child_item){
                  $childs[] = $child_item->id;
               }
            }
            //CVarDumper::dump($childs,10,true);die();
            $criteria = new CDbCriteria();
            $top_cate = array(6,9,7); // Những cate này sẽ lấy ra 22 tin
            $criteria->addCondition('active = 1');
            $criteria->addInCondition('cat_id',$childs);
            $criteria->order = 'id DESC';
            if(in_array($item->id,$top_cate)){
               $criteria->limit = 22;
            }else{
               $criteria->limit = 10;   
            }
            $tmp['list_news'] = News::model()->findAll($criteria);
            $block_news[] = $tmp;
            //$tmp = array();
            //CVarDumper::dump($block_news,10,true);die();
         }
         
      }
      
      $this->pageTitle = 'Bongda102.com - Tin tức bóng đá 24/7';
      $description = 'Tin tức mới nhất, nóng nhất, hay nhất, tin tức bóng đá cập nhật liên tục 24/7 , troll bóng đá, chém gió về bóng đá, bình luận, bình luận về bóng đá';
      $keyword = 'bóng đá, bong da, troll bóng đá , troll bong da, bongda';
      //Meta tag for facebook
      Yii::app()->clientScript->registerMetaTag($this->pageTitle, 'og:title');
      Yii::app()->clientScript->registerMetaTag('article', 'og:type');
      Yii::app()->clientScript->registerMetaTag($description, 'og:description');
      Yii::app()->clientScript->registerMetaTag($keyword, 'og:keywords');
      
      Yii::app()->clientScript->registerMetaTag($this->pageTitle, 'title');
      Yii::app()->clientScript->registerMetaTag('article', 'type');
      Yii::app()->clientScript->registerMetaTag($description, 'description');
      Yii::app()->clientScript->registerMetaTag($keyword, 'keywords');
      
      //CVarDumper::dump($block_news,10,true);die();
		$this->render('index',array(
         'top_news' => $top_news,
         'block_news' => $block_news,
      ));
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}

	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model=new ContactForm;
		if(isset($_POST['ContactForm']))
		{
			$model->attributes=$_POST['ContactForm'];
			if($model->validate())
			{
				$name='=?UTF-8?B?'.base64_encode($model->name).'?=';
				$subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';
				$headers="From: $name <{$model->email}>\r\n".
					"Reply-To: {$model->email}\r\n".
					"MIME-Version: 1.0\r\n".
					"Content-Type: text/plain; charset=UTF-8";

				mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers);
				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact',array('model'=>$model));
	}

	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		$model=new LoginForm;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
				$this->redirect(Yii::app()->user->returnUrl);
		}
		// display the login form
		$this->render('login',array('model'=>$model));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
}